<?php
class Home extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library ( 'form_validation' );
		$this->load->helper ( 'form' );
	}
	
	function index() {
		if (! $this->session->userdata ( 'adminName' )) {
			$data ['msg'] = '请先登录！';
			$data ['url'] = base_url () . 'admin/home/login';
			$this->load->view ( 'admin/admin_message', $data );
		} else {
			$data ['authority'] = $this->session->userdata ( 'authority' );
			$data ['adminName'] = $this->session->userdata ( 'adminName' );
			
			$this->load->view ( 'admin/header', $data );
			$this->load->view ( 'admin/nav' );
			$this->load->view ( 'admin/home' );
			$this->load->view ( 'admin/footer' );
		}
	}
	
	function login() {
		$this->load->model ( 'Madmin' );
		$data ['adminId'] = $this->session->userdata ( 'adminId' );
		$data ['msg'] = '';
		
		if ($data ['adminId']) {
			redirect ( base_url () .'admin/home' );
		} else {
			$this->form_validation->set_rules ( 'username', '用户名', 'trim|required|max_length[40]' );
			$this->form_validation->set_rules ( 'password', '密码', 'trim|required|min_length[6]|max_length[15]' );
			$this->form_validation->set_rules ( 'test', '验证码', 'trim|required|callback_checkCaptchaCB' );
			
			if ($this->form_validation->run () == TRUE) {
				$query = $this->Madmin->getAdminforLogin ( $this->input->post ( 'username' ), md5 ( $this->input->post ( 'password' ) ) );
				if ($query) {
					$this->session->set_userdata ( 'authority', $query ['authority'] );
					$this->session->set_userdata ( 'adminName', $query ['username'] );
					$this->session->set_userdata ( 'adminId', $query ['id'] );
					redirect ( base_url () .'admin/home' );
				} else {
					$data ['msg'] = '用户名或密码错误';
				}
			}
			$data ['img'] = $this->getCaptcha ();
			$data ['form'] = array ('id' => 'adminLoginForm' );
			$this->load->view ( 'admin/admin_login', $data );
		}
	}
	
	function register() {
		if (($this->session->userdata ( 'authority' ) === '0')) {
			$this->form_validation->set_rules ( 'username', '用户名', 'trim|required|max_length[40]|callback_checkUsernameCB' );
			$this->form_validation->set_rules ( 'password', '密码', 'trim|required|min_length[6]|max_length[15]' );
			$this->form_validation->set_rules ( 'rePassword', '确认密码', 'trim|required|min_length[6]|max_length[15]|callback_checkPwdCB' );
			
			if ($this->form_validation->run () == FALSE) {
				$data ['form'] = array ('id' => 'adminregisterForm' );
				
				$data ['authority'] = $this->session->userdata ( 'authority' );
				$data ['adminName'] = $this->session->userdata ( 'adminName' );
				
				$this->load->view ( 'admin/header', $data );
				$this->load->view ( 'admin/nav' );
				$this->load->view ( 'admin/admin_register' );
				$this->load->view ( 'admin/footer' );
			} else {
				$this->load->model ( 'Madmin' );
				$id = $this->Madmin->addAdmin ( $this->input->post ( 'username' ), md5 ( $this->input->post ( 'password' ) ) );
				if ($id) {
					$data ['msg'] = '管理员添加成功！';
					$data ['url'] = base_url () . 'admin/home/adminList';
				} else {
					$data ['msg'] = '管理员添加失败！';
					$data ['url'] = base_url () . 'admin/home/register';
				}
				
				$data ['authority'] = $this->session->userdata ( 'authority' );
				$data ['adminName'] = $this->session->userdata ( 'adminName' );
				
				$this->load->view ( 'admin/admin_message', $data );
			}
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有添加管理员的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	function modify() {
		$this->load->helper ( 'form' );
		$this->load->model ( 'Madmin' );
		
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1') || ($this->session->userdata ( 'authority' ) === '2')) {
			$this->form_validation->set_rules ( 'oldPassword', '原密码', 'trim|required|min_length[6]|max_length[15]|callback_checkOldPwdCB' );
			$this->form_validation->set_rules ( 'password', '新密码', 'trim|required|min_length[6]|max_length[15]' );
			$this->form_validation->set_rules ( 'rePassword', '确认密码', 'trim|required|min_length[6]|max_length[15]|callback_checkPwdCB' );
			
			if ($this->form_validation->run () == FALSE) {
				$data ['form'] = array ('id' => 'modifyForm' );
				
				$data ['authority'] = $this->session->userdata ( 'authority' );
				$data ['adminName'] = $this->session->userdata ( 'adminName' );
				
				$this->load->view ( 'admin/header', $data );
				$this->load->view ( 'admin/nav' );
				$this->load->view ( 'admin/admin_modification' );
				$this->load->view ( 'admin/footer' );
			} else {
				$pwd = md5 ( $this->input->post ( 'password' ) );
				
				if ($this->Madmin->modification ( $pwd, $this->session->userdata ( 'adminId' ) )) {
					$data ['msg'] = '修改成功！';
					$data ['url'] = base_url () . 'admin/home';
					$this->load->view ( 'admin/admin_message', $data );
				} else {
					$data ['msg'] = '修改失败！';
					$data ['url'] = base_url () . 'admin/home/modify';
					$this->load->view ( 'admin/admin_message', $data );
				}
			}
		} else {
			$data ['msg'] = '请先登录！';
			$data ['url'] = base_url () . 'admin/home/login';
			$this->load->view ( 'admin/admin_message', $data );
		}
	}
	
	function adminList() {
		if (($this->session->userdata ( 'authority' ) === '0')) {
			$this->load->model ( 'Madmin' );
			$this->load->library ( 'pagination' );
			
			$data ['num'] = $this->Madmin->getAdminNum ();
			$data ['page'] = $this->uri->segment ( 4, 0 );
			
			$config ['base_url'] = base_url () . '/admin/home/adminList/';
			$config ['total_rows'] = $data ['num'];
			$config ['per_page'] = PAGE_SHOW_NUM;
			$config ['uri_segment'] = 4;
			$config ['num_links'] = 2;
			$this->pagination->initialize ( $config );
			
			$data ['list'] = $this->Madmin->getAdminList ( $data ['page'], PAGE_SHOW_NUM );
			
			$data ['authority'] = $this->session->userdata ( 'authority' );
			$data ['adminName'] = $this->session->userdata ( 'adminName' );
			
			$this->load->view ( 'admin/header', $data );
			$this->load->view ( 'admin/nav' );
			$this->load->view ( 'admin/admin_list' );
			$this->load->view ( 'admin/footer' );
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有管理管理员的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	function deleAdmin() {
		if ($this->session->userdata ( 'authority' ) === '0') {
			$data ['id'] = $this->uri->segment ( 4, 0 );
			$this->load->model ( 'Madmin' );
			
			if ($this->Madmin->deleteAdmin ( $data ['id'] )) {
				$data ['msg'] = '管理员删除成功！';
			} else
				$data ['msg'] = '管理员删除失败！';
			
			$data ['url'] = base_url () . 'admin/home/adminList';
			$this->load->view ( 'admin/admin_message', $data );
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有删除管理员的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	function changeAu() {
		if ($this->session->userdata ( 'authority' ) === '0') {
			$id = $this->uri->segment ( 4, 0 );
			$data ['authority'] = $this->uri->segment ( 5, 2 );
			$this->load->model ( 'Madmin' );
			
			if ($this->Madmin->updateAdmin ( $data, $id )) {
				$data ['msg'] = '权限修改成功！';
			} else
				$data ['msg'] = '权限修改失败！';
			
			$data ['url'] = base_url () . 'admin/home/adminList';
			$this->load->view ( 'admin/admin_message', $data );
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有权限修改的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	// 功能函数
	function getCaptcha() {
		$this->load->helper ( 'captcha' );
		$this->load->helper ( 'string' );
		$vals = array ('word' => random_string ( 'numeric', 5 ), 'img_path' => './captchas/', 'img_url' => base_url () . 'captchas/', 'img_width' => 60, 'img_height' => 30, 'expiration' => 120 );
		$cap = create_captcha ( $vals );
		$this->session->set_userdata ( 'verifyCode', $cap ['word'] );
		return $cap ['image'];
	}
	
	function checkCaptchaCB($str) {
		if (strcasecmp ( $str, $this->session->userdata ( 'verifyCode' ) ) == 0) {
			return TRUE;
		} else {
			$this->form_validation->set_message ( 'checkCaptchaCB', '%s不正确' );
			return FALSE;
		}
	}
	
	function checkPwdCB($str) {
		if ($str == $this->input->post ( 'password' ))
			return TRUE;
		else {
			$this->form_validation->set_message ( 'checkPwdCB', '两次密码输入必须一致' );
			return FALSE;
		}
	}
	
	function checkUsernameCB($str) {
		$this->load->model ( 'Madmin' );
		if ($this->Madmin->checkUsername ( $str ))
			return TRUE;
		else {
			$this->form_validation->set_message ( 'checkUsernameCB', '%s已存在' );
			return FALSE;
		}
	}
	
	function checkOldPwdCB($str) {
		if ($str == '')
			return TRUE;
		else {
			$this->load->model ( 'Madmin' );
			if ($this->Madmin->checkPwdbyId ( md5 ( $str ), $this->session->userdata ( 'adminId' ) ))
				return TRUE;
			else {
				$this->form_validation->set_message ( 'checkOldPwdCB', '%s错误' );
				return FALSE;
			}
		}
	}
	
	function logout() {
		$this->session->unset_userdata ( 'authority' );
		$this->session->unset_userdata ( 'adminName' );
		$this->session->unset_userdata ( 'adminId' );
		redirect ( base_url () .'admin/home/login' );
	}
}